package A一周刷爆LeetCode.A基础班.G图;

import java.util.HashSet;
import java.util.Stack;

/**
 * @author wei.zhao
 * @description: 深度优先遍历
 * @date 2022年03月04日 14:43
 */
public class Code02_DFS {

    public static void dfs(Node head) {
        if (head == null) {
            return;
        }
        Stack<Node> stack = new Stack<>();
        HashSet<Node> set = new HashSet<>();
        stack.add(head);
        set.add(head);
        System.out.println(head.value);
        while (!stack.isEmpty()) {
            Node node = stack.pop();
            for (Node next : node.nexts) {
                if (!set.contains(next)) {
                    stack.add(node);
                    stack.add(next);
                    set.add(next);
                    System.out.println(next.value);
                    break;
                }
            }
        }
    }

}
